/********************************************************************************
 * @Author: Zhenzhou Liu
 * @Date: 2024-12-29
 * @Description: Cg图论练习之有向图欧拉路径
 *******************************************************************************/
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

bool isConnected(const vector<vector<int>> & adj, int n) {
    queue<int> que;
    vector<bool> visited(n + 1, false);

    for (int i = 1; i <= n; i++) {
        if (!adj[i].empty()) {
            que.push(i);
            break;
        }
    }

    while (!que.empty()) {
        int u = que.front();
        que.pop();
        visited[u] = true;

        for (int v : adj[u]) {
            if (!visited[v]) {
                que.push(v);
            }
        }
    }
    for (int i = 1; i <= n; i++)
        if (!visited[i])
            return false;
    return true;
}

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> adj(n + 1);
    vector<int> inDegree(n + 1, 0);
    vector<int> outDegree(n + 1, 0);
    int startCount = 0, endCount = 0;
    int u, v;
    for (int i = 0; i < m; i++) {
        cin >> u >> v;
        adj[u].push_back(v);
        inDegree[v]++;
        outDegree[u]++;
    }

    for (int i = 1; i <= n; i++) {
        if (outDegree[i] == inDegree[i] + 1)
            startCount++;
        else if (inDegree[i] == outDegree[i] + 1)
            endCount++;
        else if (inDegree[i] != outDegree[i]) {
            cout << 0;
            return 0;
        }
    }

    if ((startCount != 1 || endCount != 1) && (startCount != 0 || endCount != 0)) {
        cout << 0;
        return 0;
    }

    if (!isConnected(adj, n)) {
        cout << 0;
        return 0;
    }
    cout << 1;
    return 0;
}


/*Test Case1
3 2
1 2
2 3
*/
